package ai.people.platform.archives.intimacy.handler.impl;

import ai.people.core.grpc.lib.key.account.TelegramResponse;
import ai.people.core.nebula.util.NebulaQueryUtils;
import ai.people.netmon.framework.domain.graph.enums.GraphTagEnum;
import ai.people.netmon.framework.domain.key.account.entity.intimacy.TelegramIntimacy;
import ai.people.netmon.framework.domain.key.account.enums.IntimacyFieldsEnum;
import ai.people.netmon.framework.domain.key.account.enums.IntimacySupportEnum;
import ai.people.netmon.framework.domain.key.account.vo.ExtendVO;
import ai.people.platform.archives.intimacy.IntimacyHandlerFactory;
import ai.people.platform.archives.intimacy.handler.IntimacyHandleService;
import com.alibaba.fastjson.JSONObject;
import com.vesoft.nebula.client.graph.exception.IOErrorException;
import com.vesoft.nebula.client.graph.net.SessionWrapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhenglin
 * @date 2022/7/28
 */
@Service
public class TelegramIntimacyHandleServiceImpl implements IntimacyHandleService, InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        IntimacyHandlerFactory.registerIntimacyHandler(IntimacySupportEnum.TELEGRAM.value, this);
    }

    @Override
    public List<ExtendVO.Extend> analysis(List<?> responseList, Map<String, Integer> ruleMapping, String username, SessionWrapper session) throws IOErrorException, UnsupportedEncodingException {
        List<TelegramResponse> data = (List<TelegramResponse>) responseList;
        Set<String> vidList = data.stream().map(x -> NebulaQueryUtils.buildVid(GraphTagEnum.TELEGRAM.vidPrefix, x.getUsername())).collect(Collectors.toSet());

        Map<String, Integer> sameTelegramGroupMapping = getSameTelegramGroupMapping(session, vidList, username);

        List<TelegramIntimacy> telegramIntimacies = new ArrayList<>();
        for (TelegramResponse response : data) {
            TelegramIntimacy intimacy = JSONObject.parseObject(JSONObject.toJSONString(response), TelegramIntimacy.class);

            int msgAtWight = getFieldWeight(IntimacyFieldsEnum.msg_at_cnt.name(), ruleMapping);
            int sameGroupWight = getFieldWeight(IntimacyFieldsEnum.same_group_cnt.name(), ruleMapping);
            int replayWight = getFieldWeight(IntimacyFieldsEnum.replay_cnt.name(), ruleMapping);
            int privateLetterWight = getFieldWeight(IntimacyFieldsEnum.private_letter_cnt.name(), ruleMapping);

            Integer sameGroupCnt = sameTelegramGroupMapping.getOrDefault(intimacy.getUsername(), 0);

            int qmd = intimacy.getMsgAtCnt() * msgAtWight + sameGroupCnt * sameGroupWight
                    + intimacy.getReplayCnt() * replayWight + intimacy.getPrivateLetterCnt() * privateLetterWight;
            intimacy.setQmd(qmd);
            telegramIntimacies.add(intimacy);
        }
        List<ExtendVO.Extend> res = new ArrayList<>();
        telegramIntimacies.sort(Comparator.comparingInt(TelegramIntimacy::getQmd).reversed());
        for (TelegramIntimacy intimacy : telegramIntimacies) {
            ExtendVO.Extend extendVO = new ExtendVO.Extend();
            extendVO.setUsername(intimacy.getUsername());
            extendVO.setQmd(intimacy.getQmd());

            int msgAtCnt = intimacy.getMsgAtCnt();
            int sameGroupCnt = intimacy.getSameGroupCnt();
            int replayCnt = intimacy.getReplayCnt();
            int privateLetterCnt = intimacy.getPrivateLetterCnt();

            JSONObject obj = new JSONObject();
            obj.put(IntimacyFieldsEnum.msg_at_cnt.name(), msgAtCnt);
            obj.put(IntimacyFieldsEnum.same_group_cnt.name(), sameGroupCnt);
            obj.put(IntimacyFieldsEnum.replay_cnt.name(), replayCnt);
            obj.put(IntimacyFieldsEnum.private_letter_cnt.name(), privateLetterCnt);

            extendVO.setBasis(obj);
            res.add(extendVO);
        }
        return res;
    }
}
